BlogHow to

Your Linux Terminal Can Tell You Your Fortune, Here’s How

Key Takeaways

  • You can use the “fortune” command to view random quotes, jokes, or advice right in your terminal.
  • Additionally, you can customize your fortune’s database by creating your own fortune file.
  • For additional fun, you could pipe a fortune with cowsay, or display a random fortune every time you launch your terminal.


Ever thought your terminal could be a source of daily wisdom or humor? It’s true! With the fortune command, you can receive a random quote, joke, or piece of advice every time you execute it. Whether you’re working or enjoying a break, a short fortune can lift your mood and encourage new ideas.



What Is the fortune Command?

The fortune command is a classic tool, with roots tracing back to the 1980s when it was bundled with Unix. This command’s primary purpose is to display a random quote, proverb, or humorous saying each time you run it. It is your digital fortune cookie that offers a slice of wisdom or a chuckle whenever you need it.


Why add fortunes to your terminal? It’s just for fun! You can use them in your system’s message of the day. Personally, I find the fortune command handy when I need some quick text to test regular expressions in the terminal.

To use fortune, you first need to make sure it’s already present in your system. The original version of fortune was created for a Unix-like operating system called NetBSD. However, if you’re using Linux, you can’t use that original version directly. Instead, you have to use a modified version called fortune-mod.

So, when you’re installing fortune on a Linux system, you’re actually installing the fortune-mod package. This version has been adapted specifically to work on Linux.

On all major Linux systems, you can install fortune via default package manager. For example, on Ubuntu or Debian, run:

sudo apt install fortune-mod

To get it on Fedora and other RPM-based systems, run:

sudo dnf install fortune-mod

For Arch Linux, run this:

sudo pacman -S fortune-mod


Once installed, all you need to do is type fortune into the terminal, hit enter, and wait for the magic to happen.

fortune

fortune Options

When you run a fortune without any arguments, it randomly selects an epigram from the fortune’s database. However, it also comes with several options that let you enhance your experience. Let’s check out some of them:

Option

Description

-a

This option includes both offensive and inoffensive fortunes.

-s

This option tells fortune to only display short quotes. (less than 160 characters)

-I

Serves up the long ones (more than 160 characters)

-c

Display the cookie file from which the fortune was selected.

-f

Print a list of files that will be searched, without actually printing a fortune.

-e

Using this option gives all files an equal shot. By default, longer files have a higher chance of being selected.

-i

Ignore the case when matching patterns with -m.

-m

Use this to search for fortunes that match a particular keyword. For example, if you want to see fortunes about love, you can type fortune -m love.

-n

Set the maximum length for a fortune to be considered short. The default is 160 characters. Anything longer fortune will classify as “long.”


Furthermore, fortune allows you to choose from different categories, such as, if you’re a fan of literature or riddles, you can instruct fortune to only show quotes from those categories.

fortune literature

You can also combine multiple options together, such as if you want a short, offensive quote from the literature category, run this:

fortune -s -o literature

Create Your Own fortune File

What if I told you that you could create your own fortunes? That’s right, it’s a straightforward process. All you need is to create your custom fortune file and make it compatible with the fortune command’s requirements. In this way, you’re not bound to the predefined messages.

To try it out, simply open your terminal and create a plain text document using your favorite text editor.

nano funny

Next, start adding your fortunes and make sure that each fortune is separated by a line with a percent sign % on it.


Adding fortunes text to a plain text file using nano text editor in Linux terminal.

Once your file is ready, you need to convert it to a format that fortune can use. You can do this with the strfile command, which generates the necessary “funny.dat” file for the fortune command:

strfile -c % funny funny.dat

Converting plain text file to fortune supported file using strfile command.

Now, move both files to the directory where fortune looks for its data. This location can vary, but it often looks like “/usr/share/games/fortunes/” or “/usr/local/share/games/fortunes/”.

sudo cp funny* /usr/share/games/fortunes/

You can verify the movement by running this command:

fortune -f


Listing all fortune source files using the -f option of fortune command.

The percentage here gives you a rough idea of how much of the total database size is taken up by a particular file.

Finally, you can use the fortune command to get fortunes from your customs file.

fortune funny

Displaying random saying from your newly created fortune file.

Every time you run the command, one of your hand-crafted fortunes will appear.

Combine fortune and cowsay for Extra Fun

If you think the fortune command is cool, wait until you see it combined with cowsay. This fun command displays an ASCII art cow in your terminal that says whatever you input. There’s no need for arguments—just provide some text, and you’re good to go.


You can get cowsay from your default package manager such as on Ubuntu or Debian, use apt:

sudo apt install cowsay

Now, let’s pipe fortune output with cowasy to produce something very funny and interesting.

fortune | cowsay

Piping fortune output to cowsay command to display random saying along with ASCII art.

Check it out! You’ve got a cow dispensing wisdom right there in your terminal. Just what you always wanted!

But wait—there’s more! You can also change the character delivering your fortune. For example, use -f followed by the character’s name to pick any other ASCII creature:

fortune | cowsay -f tux

Tux displays random statements using fortune and cowsay commands.


You can also make your tux colorful by piping the lolcat command.

fortune | cowsay -f tux | lolcat

Making the ouptut of fortune and cowsay colorful using the lolcat command.

View Fortune on Terminal Startup

Want to see a friendly dose of wisdom every time you open a bash terminal? If yes, then you need to add a fortune command to the end of your ~/.bashrc file.

The first thing you need to do is to edit your shell configuration file. For bash shell, the configuration file is likely ~/.bashrc, and for zsh, it’s ~/.zshrc. I’m using bash, so let’s open it with:

nano ~/.bashrc

Next, scroll to the bottom of the file and add the following line:

fortune | cowsay

Adding fortune command along with cowsay to the end of bashrc file.


This will ensure that every time you open your terminal, you’ll see a fortune delivered by a cow.

After saving changes, press Ctrl+X to exit the editor. To see the changes happen, you can either restart your terminal or execute:

source ~/.bashrc

Now, every time you launch your terminal, you’ll be greeted with a delightful fortune.

Random fortune appearing on a Linux terminal after being launched.

If you want to use different ASCII art creatures, such as tux, just replace the cowsay command in the ~/.bashrc file with the cowsay -f tux command.

Adding new fortune command with tux ASCII art to the end of bashrc file.


Use Fortune for Testing

Beyond its entertainment or enlightening uses, the fortune command has a few hidden applications. For example, you can often use it to generate random text files that are very useful for testing scripts and various commands.

Let’s redirect fortune output to a file:

fortune > file1

To view the content of the file, run:

cat file1

Viewing the file using the cat command.

If you want to append multiple fortunes to the same file, you can do so in a loop.

for i in {1..10};
do fortune >> file1;
done

Another clever use for fortune is generating random numbers. By using the wc command with the -c option, you can count the characters in each fortune:

fortune | wc -c

Create a Random Picker Game

You can also use fortune to randomize data for other purposes. Say you have a list of friends, coworkers, or contest participants—just put their names into a file and let fortune pick a winner. To accomplish this, you’ll need to make your own fortune file, as explained above.



There are several fun Linux commands that can brighten your day, much like the classic fortune command. Some that I find particularly amusing is the fake Linux hacking commands.


Source link

Related Articles

Back to top button
close